package com.vnp.cooking;

import java.util.ArrayList;
import java.util.List;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListActivity;
import com.vnp.cooking.adapter.ListCommentAdapter;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.bean.ObjComment;
import com.vnp.cooking.utils.KeyboardHelper;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

public class CommentActivity extends SherlockFragmentActivity implements OnClickListener{
	// khoi tao cac thanh phan tren actionbar
	ImageButton btnBack;
	ListView lsComment;
	ListCommentAdapter lsCommentAdapter;
	// thanh comment
	EditText edtComment;
	Button btnSendComment;
	ArrayList<ObjComment> objComments = new ArrayList<ObjComment>();
	//demo avatar url
	String urlImageDemo = "http://images1.fanpop.com/images/photos/1800000/Leah-s-photoshoot-for-website-profile-leah-dizon-1856157-531-800.jpg";
	Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment);
		
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnSendComment = (Button) findViewById(R.id.btn_send_comment);
		edtComment = (EditText) findViewById(R.id.edt_create_comment);
		
		lsComment = (ListView) findViewById(R.id.list_comment);
		lsCommentAdapter = new ListCommentAdapter(this, objComments);
		lsComment.setAdapter(lsCommentAdapter);
		lsComment.setSelection(createData().size() -1);
		// set su kien click cho nut back
		btnBack.setOnClickListener(this);
		btnSendComment.setOnClickListener(this);
		
		/*Runnable r = new Runnable() {
			
			@Override
			public void run() {
				KeyboardHelper.displaySoftKeyboard(CommentActivity.this, edtComment);
			}
		};
		
		mHandler.postDelayed(r, 300);*/
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			lsCommentAdapter.notifyDataSetChanged();
			closeActivity();
			break;
		case R.id.btn_send_comment:
			addCommentToList();
			break;
		default:
			break;
		}
		
	}
	
	// tao du lieu fix cho list view
	public ArrayList<ObjComment> createData() {
		ArrayList<ObjComment> objComments = new ArrayList<ObjComment>();
		/*objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Món này dễ nấu lắm bạn ạ, thử là biết ngay..."));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nấu ăn như nhai rơm..."));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nhìn cũng đẹp đấy chứ"));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nhìn cũng đẹp đấy chứ"));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nhìn cũng đẹp đấy chứ"));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nhìn cũng đẹp đấy chứ"));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nhìn cũng đẹp đấy chứ"));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nhìn cũng đẹp đấy chứ"));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nhìn cũng đẹp đấy chứ"));
		objComments.add(new ObjComment(urlImageDemo,"Thientvse", "5h", "Nhìn cũng đẹp đấy chứ"));
	*/	
		return objComments;
	}
	
	public void addCommentToList() {
		if (edtComment.getText().toString().matches("")){
			
		} else {
			ObjComment objComment = new ObjComment(urlImageDemo, "Thientv", "1h", edtComment.getText().toString());
			objComments.add(objComment);
			edtComment.setText("");
			lsComment.setSelection(objComments.size());
		}
		
//		lsCommentAdapter.notifyDataSetChanged();
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}


	@Override
	public void onBackPressed() {
		closeActivity();
	}


}
